import * as db from './db.js';
//操作失败的提示信息
function errorToShow(msg = '操作失败', callback = function() {}) {
	uni.showToast({
		title: msg,
		icon: 'none',
		duration: 1500
	})
	setTimeout(function() {
		callback()
	}, 1500)
}

//操作成功后，的提示信息
function successToShow(msg = '保存成功', callback = function() {}) {
	uni.showToast({
		title: msg,
		icon: 'success',
		duration: 1000
	})
	setTimeout(function() {
		callback()
	}, 500)
}
//跳转到登陆页面
function jumpToLogin(method) {
	var now_time = Date.parse(new Date())
	var value = db.get('jump_to_login')
	if (!value) {
		value = 0
	}
	if (now_time - value > 3000) {
		db.set('jump_to_login', now_time); //暂时屏蔽登录时间查询
		uni.showToast({
			title: '请先登录!',
			icon: 'none',
			duration: 1000,
			success: function(res) {
				setTimeout(() => {
					uni.hideToast();
					uni.switchTab({
						url: '/pages/user/user',
						animationType: 'pop-in',
						animationDuration: 200
					})
				}, 500)
			}
		})
	}
}

function throttle(fn, gapTime) {
	if (gapTime == null || gapTime == undefined) {
		gapTime = 1500
	}

	let _lastTime = null

	// 返回新的函数
	return function() {
		let _nowTime = +new Date()
		if (_nowTime - _lastTime > gapTime || !_lastTime) {
			fn.apply(this, arguments) //将this和参数传给原函数
			_lastTime = _nowTime
		}
	}
}

function timestampToTime(timestamp) {
	var date = new Date(timestamp * 1000);
	var Y = date.getFullYear() + '-';
	var M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + '-';
	var D = date.getDate() + ' ';
	var h = date.getHours() + ':';
	var m = date.getMinutes() + ':';
	var s = date.getSeconds();
	return Y + M + D + h + m + s;
}
/**
 * 统一跳转
 */
function navigateTo(url, callback = function() {}) {
	uni.navigateTo({
		url: url,
		animationType: 'pop-in',
		animationDuration: 300
	})
	callback()
}

function redirectTo(url, callback = function() {}) {
	uni.redirectTo({
		url: url,
		animationType: 'pop-in',
		animationDuration: 300
	})
	callback()
}
/**
 * tabber跳转
 */
function switchTabTo(url, callback = function() {}) {
	uni.switchTab({
		url: url,
		animationType: 'pop-in',
		animationDuration: 200
	})
	callback()
}
/**
 * 计算百分比
 */
function GetPercent(num, total) {
	num = parseFloat(num);
	total = parseFloat(total);
	if (isNaN(num) || isNaN(total)) {
		return "-";
	}
	return total <= 0 ? "0%" : (Math.round(num / total * 10000) / 100.00) + "%";
}
/**
 *  过滤非法字符
 */
function filterNum(i) {
	let num = i.charAt(i.length - 1)
	var reg = new RegExp("^[0-9]*$")
	if (!reg.test(num)) {
		return i.slice(0, -1)
	} else {
		return i
	}
}
/**
 * 随机获取数组中一个元素
 */
function getRandomArrayElements(arr, count) {
	var shuffled = arr.slice(0),
		i = arr.length,
		min = i - count,
		temp, index;
	while (i-- > min) {
		index = Math.floor((i + 1) * Math.random());
		temp = shuffled[index];
		shuffled[index] = shuffled[i];
		shuffled[i] = temp;
	}
	return shuffled.slice(min);
}

export {
	errorToShow,
	successToShow,
	jumpToLogin,
	throttle,
	timestampToTime,
	navigateTo,
	GetPercent,
	filterNum,
	switchTabTo,
	redirectTo,
	getRandomArrayElements
}
